package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    /*
    * 套餐管理分页查询
    *
    * */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name){

        //1、先查询套餐的分页数据
        //构造分页构造器 new Page
        Page<Setmeal> pageInfo1 = new Page<>(page, pageSize);

/*        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        //like 模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(name),"name",name);*/

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        // 以修改时间作为排序时间
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        // pageInfo1 分页条件  queryWrapper 查询条件
        setmealService.page(pageInfo1,queryWrapper);

        //2、将Page<Setmeal> pageInfo1 转换为 Page<SetmealDto> pageInfo2
        Page<SetmealDto> pageInfo2 = new Page<>(page, pageSize);

        //2.1 拷贝total、page
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        //2.2 拷贝records
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal1 : pageInfo1.getRecords()) {
            //将没一个setmeal转换为setmealDto
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal1,setmealDto);
            //TODO 设置分类名称  category 分类信息
            Category category = categoryService.getById(setmealDto.getCategoryId());
            //                          获取分类名称
            setmealDto.setCategoryName(category.getName());
            //将setmealDto添加到setmealDtoList集合中
            setmealDtoList.add(setmealDto);

        }
        pageInfo2.setRecords(setmealDtoList);

        return R.success(pageInfo2);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /*
    *  停售套餐
    * */
    @PostMapping("status/{status}")
    public R<String> sales(@PathVariable Integer status,Long ids){
        // new 一个条件构造器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        // eq 一对一 相等  in 是只要有一个相等就行
        queryWrapper.eq("id",ids);
        // 获取唯一的 ids
        Setmeal setmeal = setmealService.getOne(queryWrapper);
        // 修改 status  售卖状态
        setmeal.setStatus(status);
        // 根据id修改
        setmealService.updateById(setmeal);
        return R.success("停售成功");
    }


    /*
    * 批量修改状态
    *
    * */
/*    @PostMapping("status/{status}")
    public R<String> sales(@PathVariable Integer status,Long[] ids){
        return null;
    }*/

    /*
    * 根据分类ID查询套餐列表
    * */
    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal){

        //
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",setmeal.getCategoryId());
        queryWrapper.eq("status",setmeal.getStatus());
        queryWrapper.orderByDesc("update_time");

        List<Setmeal> setmealList = setmealService.list(queryWrapper);
        return  R.success(setmealList);
    }

    @GetMapping("dish")
    public R<List<DishDto>> dish(@PathVariable Long id){


        return null;
    }



}
